package kr.hkit.powerjava.chapter12.Programming;

import java.util.Arrays;
import java.util.Comparator;

public class PgMain {
	
	public static void main(String[] args) {
		
		Shape arrayOfShapes[];
		
		arrayOfShapes = new Shape[3];
		
		arrayOfShapes[0] = new Rectangle();
		arrayOfShapes[1] = new Triangle();
		arrayOfShapes[2] = new Circle();
		
		arrayOfShapes[2].setX(5);
		arrayOfShapes[1].setX(2);
		arrayOfShapes[1].setY(2);
		
		for (int i = 0; i < arrayOfShapes.length; i++) {
			arrayOfShapes[i].move(5, 5);
			System.out.println();
		}
		
		for (int i = 0; i < arrayOfShapes.length; i++) {
			arrayOfShapes[i].draw();
		}
		
		Person arrPerson[];
		
		arrPerson = new Person[3];
		
		arrPerson[0] = new Person("김모모", 168);
		arrPerson[1] = new Person("이모모", 180);
		arrPerson[2] = new Person("박모모", 170);
		
		Person per = new Person("최모모", 165);
		
		for (int i = 0; i < arrPerson.length; i++) {
			System.out.println(arrPerson[i]);
		}
		System.out.println(per);
		
		System.out.println(arrPerson[0].equals(new Person("김모모")));
				
		System.out.println();
		System.out.printf("키가 가장 큰 사람은 %s이다", getMaximum(arrPerson));
		System.out.println();
		
		
		
		Arrays.sort(arrPerson);
		prnArr(arrPerson);
		Person p = new Person("박모모");
		int a = Arrays.binarySearch(arrPerson,p);
		System.out.println(a>0? "Found":"NotFound");
		
		Book arrBook[] = new Book[4];
		
		arrBook[0] = new Novel();
		arrBook[1] = new Poet();
		arrBook[2] = new ScienceFiction();
		arrBook[3] = new Book();
		
		arrBook[0].setTitle("AAA");
		arrBook[0].setNumber(1);
		arrBook[0].setAuthor("aa");
		
		arrBook[1].setTitle("B");
		arrBook[1].setAuthor("b");
		arrBook[1].setNumber(2);
		
		arrBook[2].setTitle("c");
		arrBook[2].setNumber(3);
		
		arrBook[3].setTitle("D");
		arrBook[3].setNumber(2);
		
		for (int i = 0; i < arrBook.length; i++) {
			System.out.println(arrBook[i]);
		}
		
		System.out.println("책 AAA와 D는 같은가? ");
		System.out.println(arrBook[0].equals(arrBook[3])? "같다":"다르다");
		System.out.println("책 B와 D는 같은가? ");
		System.out.println(arrBook[1].equals(arrBook[3])?"같다":"다르다");
		
		System.out.println("AAA책을 3일 연체한 경우 연체료는 " + ((Novel)arrBook[0]).getLateFees(3) + "원이다.");
	}
	
	public static String getMaximum(Person[] array){
		
		Comparator<Person> hSort = new Comparator<Person>() {

			@Override
			public int compare(Person o1, Person o2) {
				return (int) (o1.getHeight()*100- o2.getHeight()*100);
			}
		};
		
		
		Arrays.sort(array, hSort);
		prnArr(array);
		return array[array.length-1].getName();
	}

	private static void prnArr(Person[] array) {
		for(Person p : array){
			System.out.println(p);
		}
	}
	
}
